let idx = -1; //用做唯一的wx:key
Component({
  options: {
    addGlobalClass: true,
  },
  properties: {
    // _temp: {
    //   type: Object,
    //   value: {},
    //   observer: function (newVal, oldVal) {
    //     console.log(newVal,'newVal',oldVal,'oldVal');
    //     if (!newVal || Object.keys(newVal).length === 0) {
    //       return;
    //     }
    //     console.log('新弹幕数据:', newVal);
    //   }
    // }
  },
  data: {
    box: [],
    _usedTops: [],
  },
  methods: {
    autoRemoveBarrage() {
      // 如果box最后一项是undefined，就不执行
      const box = this.data.box;
      if (box.length > 0 && typeof box[box.length - 1] === "undefined") {
        return;
      }
      console.log("autoRemoveBarrage");
      const now = Date.now();
      const newBox = this.data.box;
      for (let i = 0; i < box.length; i++) {
        const barrage = box[i];
        if (barrage && barrage.createTime) {
          if (now - barrage.createTime >= 12000) {
            newBox[i] = undefined;
          }
        }
      }
      this.setData({
        box: newBox,
      });
      console.log(this.data.box, "this.data.box");
    },
    // 保证弹幕高度(top)随机且不重复
    getUniqueTop() {
      // 用box后面十项的数据来进行比较，确保top值不重复
      const box = this.data.box || [];
      // 取box最后10项（不足10项则全部取出）
      const recentBox = box.slice(-10);
      // 提取最近10项的top值
      const recentTops = recentBox
        .map((item) => item && item.top)
        .filter((top) => typeof top === "number");
      let top;
      let tryCount = 0;
      do {
        top = Math.ceil(Math.random() * 100);
        tryCount++;
        // 最多尝试100次，防止死循环
        if (tryCount > 100) break;
      } while (recentTops.some((recentTop) => Math.abs(recentTop - top) <= 3));
      return top;
    },
    create(object) {
      let obj = {
        text: object.text,
        status: object.status,
        gift: object.gift || 0,
        id: "a" + idx,
        top: this.getUniqueTop(),
        time: Number(Math.random() * 5) + 5,
        createTime: Date.now(),
        color: this.getRandomColor(),
      };
      console.log("time", obj.time);
      return obj;
    },

    getRandomColor() {
      const color_arr = [
        "#esd771",
        "#dcecdd",
        "#7cbaad",
        "#7a83b3",
        "#bb5644",
        "#ebb282",
        "#79b1d8",
        "#98d0de",
        "#e4844e",
        "#85fefe",
        "#fb5422",
        "#f6d087",
      ];
      let i = parseInt(Math.random() * 5);
      return color_arr[i];
    },

    test() {
      idx = idx + 1;
      let obj = {
        text: "LeeYa祝你恭喜发财" + idx + "次",
        status: "like",
        gift: 0,
      };
      let createBox = this.create(obj);
      console.log("createBox", createBox);
      let box = this.data.box;
      box.push(createBox);
      this.setData({
        box,
      });
    },
  },
  // 组件生命周期
  lifetimes: {
    attached() {
      this._autoRemoveTimer = setInterval(() => {
        this.autoRemoveBarrage();
      }, 12000);
    },
    detached() {
      if (this._autoRemoveTimer) {
        clearInterval(this._autoRemoveTimer);
        this._autoRemoveTimer = null;
      }
    },
  },
});
